package com.clkj.xiuzheng.dao.shangcheng;

import com.clkj.xiuzheng.pojo.shangcheng.Order;
import com.clkj.xiuzheng.pojo.shangcheng.OrderInfo;
import com.clkj.xiuzheng.pojo.shangcheng.vo.RateVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
public interface ScOrderMapper extends Mapper<Order> {
    @Select({"<script>",
            "SELECT o.*,u.user_name,u.user_phone,u2.operator_name as sendUserName FROM t_xz_order o " +
                    "left join t_xz_user u on o.user_id = u.user_id " +
                    "left join t_xz_operator u2 on o.send_user_id =u2.operator_id ",
                    " WHERE 1=1",
            "<if test = 'companyId!=null'>",
            "and o.company_id = #{companyId} ",
            "</if>",
            "<if test = 'sendOrder!=null'>",
            "and o.send_order =#{sendOrder} ",
            "</if>",
            "<if test = 'account!=null'>",
            "and o.customer_tel = #{account}",
            "</if>",
            "<if test = 'factoryId!=null'>",
            "and o.factory_id = #{factoryId}",
            "</if>",
            "order by o.order_date desc",
            "</script>"
        })
    List<Order> getOrderlist(@Param("companyId") Integer companyId, @Param("sendOrder") String sendOrder, @Param("account") String account, @Param("factoryId") String factoryId);

    @Select("select o.*,p.format_name,p.product_id,p2.product_name productName from t_xz_orderInfo o left join t_xz_productFormat p on o.format_id =p.format_id left join t_xz_product p2 on p.product_id = p2.product_id where o.order_number = #{orderNumber}")
    List<OrderInfo> getOrderInfoById(@Param("orderNumber") String orderNumber);

    @Insert("insert into t_xz_order "+
            "values (#{orderNumber},#{customerName},#{customerTel},#{customerAddress},#{totalMoney},#{totalNum},#{realMoney},#{usedBalance},#{deductSource}," +
            "#{userId},#{companyId},#{orderDate},#{orderStatus},#{payDate},#{sendDate},#{sendCompany},#{sendOrder},#{takeDate},#{orderNote},#{sendUserId},#{orderFlag}," +
            "#{sendComName},#{numberFlag},#{totalPost},#{totalProductMoney},#{factoryId})" )
    @Options(useGeneratedKeys = true, keyProperty = "order_id", keyColumn = "orderId")
    void addOrder(Order order);

    @Select("select sum(total_money) from t_xz_order where company_id = #{companyId}")
    Double selectOrderTotal(Integer companyId);
    @Select("select sum(total_money) from t_xz_order where company_id = #{companyId} and order_date between #{startTime} and #{endTime} and order_status !=0 and order_status!=4 ")
    Double selectTodayOrderTotal(@Param("companyId") Integer companyId, @Param("startTime") String startTime, @Param("endTime") String endTime);
    @Select("select count(order_id) from t_xz_order where company_id = #{companyId} and order_date between #{startTime} and #{endTime} and order_status !=0 and order_status!=4")
    Integer selectTodayOrderNum(@Param("companyId") Integer companyId, @Param("startTime") String startTime, @Param("endTime") String endTime);
    @Select("select count(order_id) from t_xz_order where company_id = #{companyId} and order_status !=0 and order_status!=4")
    Integer selectOrderNum(Integer companyId);
    @Select("select count(order_id) from t_xz_order where company_id = #{companyId} and order_date between #{startTime} and #{endTime} and order_status !=0 and order_status!=4 ")
    Integer selectOrderByMonthDate(@Param("companyId") Integer companyId, @Param("startTime") String startTime, @Param("endTime") String endTime);
    @Select("select count(order_id) from t_xz_order where company_id = #{companyId} and order_date between #{startTime} and #{endTime} and order_status !=0 and order_status!=4")
    Integer selectOrderByWeekDate(@Param("companyId") Integer companyId, @Param("startTime") String startTime, @Param("endTime") String endTime);
    @Select("select TOP 15 * from t_xz_order where company_id = #{companyId} and order_status !=0 and order_status!=4  order by order_date desc")
    List<Order> getOrderList(Integer id);
    @Select("SELECT count(o.product_num) product_num,c.category_name FROM  t_xz_orderInfo o left join t_xz_product p on o.product_id = p.product_id left join t_xz_productCategory c on c.product_category_id = p.category_one_id left join t_xz_order r on o.order_number=r.order_number where o.company_id = #{companyId} and r.order_status!=0 and r.order_status!=4  GROUP BY c.category_name ")
    List<RateVo> getRateVo(Integer companyId);
    @Select("select sum(i.product_num) from t_xz_order o left join t_xz_orderInfo i on o.order_number = i.order_number  where o.factory_id = #{factoryId}")
    Integer getSalesByFactoryId(Integer factoryId);
    @Select("select sum(product_num) from t_xz_orderInfo  where format_id=#{formatId}")
    Integer getSalesByProductId( Integer formatId);
}
